package com.zbkj.crmeb.store.controller;

import com.common.CommonPage;
import com.common.CommonResult;
import com.common.PageParamRequest;
import com.exception.CrmebException;
import com.zbkj.crmeb.store.model.StoreProduct;
import com.zbkj.crmeb.store.model.StoreProductReward;
import com.zbkj.crmeb.store.request.StoreCopyProductRequest;
import com.zbkj.crmeb.store.request.StoreProductRequest;
import com.zbkj.crmeb.store.request.StoreProductRewardRequest;
import com.zbkj.crmeb.store.request.StoreProductRewardSearchRequest;
import com.zbkj.crmeb.store.request.StoreProductSearchRequest;
import com.zbkj.crmeb.store.request.StoreProductStockRequest;
import com.zbkj.crmeb.store.response.StoreProductResponse;
import com.zbkj.crmeb.store.response.StoreProductRewardResponse;
import com.zbkj.crmeb.store.response.StoreProductTabsHeader;
import com.zbkj.crmeb.store.service.StoreCartService;
import com.zbkj.crmeb.store.service.StoreProductRewardService;
import com.zbkj.crmeb.store.service.StoreProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * 分销商品佣金表 前端控制器 +---------------------------------------------------------------------- | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +---------------------------------------------------------------------- | Copyright (c) 2016~2020 https://www.crmeb.com All rights
 * reserved. +---------------------------------------------------------------------- | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +---------------------------------------------------------------------- | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Slf4j
@RestController
@RequestMapping("api/admin/store/product/reward")
@Api(tags = "商品") //配合swagger使用
public class StoreProductRewardController {

    @Autowired
    private StoreProductRewardService storeProductRewardService;

    @ApiOperation(value = "新增")
    @PostMapping(value = "/add")
    public CommonResult<String> save(@RequestBody StoreProductRewardRequest request) {
        // 检查商品是否已绑定
        checkProductBind(request);
        StoreProductReward data = new StoreProductReward();
        BeanUtils.copyProperties(request, data);
        storeProductRewardService.save(data);
        return CommonResult.success();
    }

    @ApiOperation(value = "修改")
    @PostMapping(value = "/update")
    public CommonResult<String> update(@RequestBody StoreProductRewardRequest request) {
        // 检查商品是否已绑定
        checkProductBind(request);
        StoreProductReward data = new StoreProductReward();
        BeanUtils.copyProperties(request, data);
        storeProductRewardService.updateById(data);
        return CommonResult.success();
    }

    // 检查商品是否已绑定
    private void checkProductBind(StoreProductRewardRequest request) {
        StoreProductReward data = storeProductRewardService.getByGoodIdNeId(request.getGoodId(), request.getId());
        if (data != null) {
            throw new CrmebException("商品已被设置，请重新选择商品");
        }
    }

    @ApiOperation(value = "删除")
    @GetMapping(value = "/delete")
    public CommonResult<String> delete(@RequestParam Integer id) {
        StoreProductReward data = storeProductRewardService.getById(id);
        if (data != null) {
            storeProductRewardService.removeById(id);
        }
        return CommonResult.success();
    }

    @ApiOperation(value = "详情")
    @GetMapping(value = "/detail")
    public CommonResult<StoreProductRewardResponse> detail(@RequestParam Integer id) {
        StoreProductRewardResponse shopResponse = new StoreProductRewardResponse();
        StoreProductReward data = storeProductRewardService.getById(id);
        BeanUtils.copyProperties(data, shopResponse);
        return CommonResult.success(shopResponse);
    }

    @ApiOperation(value = "分页列表")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public CommonResult<CommonPage<StoreProductRewardResponse>> getList(
        @Validated StoreProductRewardSearchRequest request,
        @Validated PageParamRequest pageParamRequest) {
        return CommonResult.success(CommonPage.restPage(storeProductRewardService.getList(request, pageParamRequest)));
    }

}



